package com.kelp.codetoolkit.editor

import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.fileEditor.FileEditorLocation
import com.intellij.openapi.fileEditor.FileEditorState
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.components.JBList
import com.intellij.ui.components.JBScrollPane
import com.intellij.util.ui.JBUI
import com.kelp.codetoolkit.data.SpringConfigListItem
import com.kelp.codetoolkit.parser.SpringYmlParser
import com.kelp.codetoolkit.renderer.SpringConfigListCellRenderer
import com.kelp.codetoolkit.ui.form.SpringConfigItemPanel
import java.awt.BorderLayout
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.beans.PropertyChangeListener
import javax.swing.DefaultListModel
import javax.swing.JComponent
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JTextField
import javax.swing.ListSelectionModel
import javax.swing.event.ListSelectionEvent
import javax.swing.event.ListSelectionListener

/**
 * @author: liuyuebai
 * @date: 2025/9/9 18:44
 * @description:
 */
class SpringConfigFileEditor(private val project: Project, private val virtualFile: VirtualFile):FileEditor{

    private val rootPanel = JPanel(BorderLayout())
    //水平分割方向 → 产生一条 竖线分隔条（用于左右两个面板） 左侧占30%
    private val splitter = SpringConfigSplitter(false, 0.1f)
    private val listModel = DefaultListModel<String>()
    private val rootList = JBList(listModel)
    private val contentPanel = JPanel(BorderLayout())

    private val springYmlParser = SpringYmlParser.getInstance(project)
    private var configData = springYmlParser.parseYamlToMap(virtualFile)

    init {
        initUI()
        loadConfigData()
    }

    private fun initUI() {
        // 左侧列表初始化
        rootList.selectionMode = ListSelectionModel.SINGLE_SELECTION
        val listScrollPane = JBScrollPane(rootList)
        listScrollPane.border = JBUI.Borders.empty(2)

        // 设置分割面板
        splitter.firstComponent = listScrollPane
        splitter.secondComponent = contentPanel

        rootPanel.add(splitter, BorderLayout.CENTER)

        // 列表选择监听，选择不同节点时更新右侧内容
        rootList.addListSelectionListener(object : ListSelectionListener {
            override fun valueChanged(e: ListSelectionEvent) {
                if (!e.valueIsAdjusting) {
                    val selectedKey = rootList.selectedValue ?: return
                    displayConfigContent(selectedKey,configData)
                }
            }
        })
    }

    private fun loadConfigData() {
        listModel.clear()
        // 将根节点（如spring、wechat等）加入左侧列表
        configData.keys.forEach { listModel.addElement(it) }
        if (listModel.size > 0) {
            rootList.selectedIndex = 0
        }
    }

    /**
     * 递归处理对象或者列表
     */
    private fun displayConfigContent(key: String,valueMap: Map<*, *>) {
        contentPanel.removeAll()
        val subValueMap = valueMap[key]?:return
        val itemPanel = SpringConfigItemPanel(project, subValueMap)
        contentPanel.add(itemPanel, BorderLayout.CENTER)
        contentPanel.revalidate()
        contentPanel.repaint()
    }

    override fun getComponent(): JComponent = rootPanel

    override fun getPreferredFocusedComponent(): JComponent? = rootList

    override fun getName(): String = "Spring Config Editor"

    override fun setState(state: FileEditorState) {}

    override fun isModified(): Boolean = false

    override fun isValid(): Boolean = true

    override fun selectNotify() {}

    override fun deselectNotify() {}

    override fun getFile(): VirtualFile {
        return virtualFile
    }

    override fun addPropertyChangeListener(listener: PropertyChangeListener) {}

    override fun removePropertyChangeListener(listener: PropertyChangeListener) {}

    override fun getCurrentLocation(): FileEditorLocation? = null

    override fun dispose() {}

    override fun <T : Any?> getUserData(key: Key<T>): T? = null

    override fun <T : Any?> putUserData(key: Key<T>, value: T?) {}
}